$(document).ready(function() {
	$("#login").click(function(evt) {
		evt.preventDefault();
		if ($("#loginbar").is(":visible"))
			$("#loginbar").hide();
		else
			$("#loginbar").show();
	});
	
	
	$("#login_btn").click(function(evt) {
		evt.preventDefault();
		var email = $("#login-email").val();
		var password = $("#login-password").val();
		if (email == "" || password == "") {
			$("#tips_login").html("Email or password cannot be empty.");
			return;
		}
		$.ajax({
			url : "login.do?email=" + email + "&passowrd=" + password,
			dataType : 'text',
			success : function(data) {
				if (data != "") {
					$(location).attr('href', data);
				}
				else {
					$("#tips_login").html("The email and password you entered do not match any accounts on record.");
				}
			},
			error : function(xhr, testStatus, error) {
				$("#tips_login").html("Communication error!");
				return;
			}
		});
	});
	
	$("#register_btn").click(function(evt) {
		evt.preventDefault();
		var vali = true;
		$("#register-form .txt").each(function() {
			if (vali) {
				if ($(this).val() == "" || $(this).parent().next().html() != "")
					vali = false;
			}
		});
		if (vali)
			$("#register-form").submit();
	});
	
	
	
	var vfname = function() {
		if ($("#fname").val() == "") {
			$("#fname").parent().next().html(getMessage("error", "First Name cannot be empty!"));
			return false;
		}
		else {
			$("#fname").parent().next().html("");
			return true;
		}
	};
	
	var vlname = function() {
		if ($("#lname").val() == "") {
			$("#lname").parent().next().html(getMessage("error", "Last Name cannot be empty!"));
			return false;
		}
		else {
			$("#lname").parent().next().html("");
			return true;
		}
	};
	
	var vemail = function() {
		var re = /^[\w\d._-]+@[\w\d.-]+\.[\w]{2,4}$/i;
		if ($("#email").val() == "") {
			$("#email").parent().next().html(getMessage("error", "Email cannot be empty!"));
			return false;
		}
		else if (!$("#email").val().match(re)) {
			$("#email").parent().next().html(getMessage("error", "Invalid email format!"));
			return false;
		}
		else {
			$("#email").parent().next().html("");
			return true;
		}
	};
	
	var vpass = function() {
		if ($("#password").val() == "") {
			$("#password").parent().next().html(getMessage("error", "Password cannot be empty!"));
			return false;
		} 
		else if ($("#password").val().length < 6) {
			$("#password").parent().next().html(getMessage("error", "Mininum length should be 6!"));
			return false;
		}
		else {
			$("#password").parent().next().html("");
			return true;
		}
	};
	
	var vrepass = function() {
		if ($("#repassword").val() != $("#password").val()) {
			$("#repassword").parent().next().html(getMessage("error", "Password do not match!"));
			return false;
		}
		else {
			$("#repassword").parent().next().html("");
			return true;
		}
	};
	
	var vorg = function() {
		if ($("#organization").val() == "") {
			$("#organization").parent().next().html(getMessage("error", "Organization cannot be empty!"));
			return false;
		}
		else {
			$("#organization").parent().next().html("");
			return true;
		}
	};
	
	$("#fname").focusout(vfname);
	
	$("#lname").focusout(vlname);
	
	$("#email").focusout(vemail);
	
	$("#password").focusout(vpass);
	
	$("#repassword").focusout(vrepass);
	
	$("#organization").focusout(vorg);
});

function getMessage(sign, message) {
	return "<div class='tip_" + sign + "'><span class='icon_" + sign + "'></span>" + message + "</div>";
}